<template>
  <div class="container">
    <el-card>
      <el-row >
        <div class="el-col el-col-18">
          <span style="color:rgb(255, 197, 218);font-size:12px">说明：目前支持学科和关键字条件筛选
</span>
        </div>
    <div class="el-col el-col-6" style="text-align: right">
      <el-button type="success" size="small" icon="el-icon-edit" @click="$router.push('new')">新增试题</el-button>
    </div>
      </el-row>
      <el-form label-width="80px" style="margin-top: 15px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select v-model="page.subjectID" placeholder="请选择">
                <el-option
                  v-for="item in simples"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="page.catalogID" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="page.tags" placeholder="请选择">
                <el-option
                  v-for="item in lables"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="page.keyword" style="width: 90%" placeholder="根据题干搜索"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="page.questionType" placeholder="请选择">
                <el-option
                  v-for="item in questionType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="page.difficulty" placeholder="请选择">
                <el-option
                  v-for="item in difficulty"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="page.direction" placeholder="请选择">
                <el-option
                  v-for="item in direction"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="page.creatorID" placeholder="请选择">
                <el-option
                  v-for="item in creators"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="page.remarks" style="width: 90%" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="page.shortName" style="width: 90%" />
            </el-form-item>
          </el-col>
          <el-col :span="3" style="width: 15%">
            <el-form-item label="城市">
              <el-select @change="provincesList" v-model="page.province" placeholder="请选择">
                <el-option
                  v-for="item in province"
                  :key="item.name"
                  :label="item.name"
                  :value="item.name"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="margin-left: 5px">
            <el-select v-model="page.city" placeholder="请选择">
              <el-option
                v-for="item in provincesList"
                :key="item.name"
                :label="item.name"
                :value="item.name"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="6">
            <el-button size="small" style="margin-left: 200px">清除</el-button>
            <el-button type="primary" size="small" @click="submitForm">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="全部" name="first"></el-tab-pane>
        <el-tab-pane label="待审核" name="second"></el-tab-pane>
        <el-tab-pane label="已审核" name="third"></el-tab-pane>
        <el-tab-pane label="已拒绝" name="fourth"></el-tab-pane>
      </el-tabs>
      <el-alert :title="'数据一共' + counts + '条'" type="info" show-icon>
      </el-alert>
      <!-- 表格组件 -->
      <el-table
        v-loading="loading"
        :data="items"
        style="width: 100%; margin-top: 10px"
      >
        <el-table-column prop="number" label="试题编号" width="120">
        </el-table-column>
        <el-table-column prop="subject" label="学科" width="120">
        </el-table-column>
        <el-table-column prop="catalog" label="目录" width="120"> </el-table-column>
        <el-table-column prop="questionType" width="120" label="题型" :formatter="formatQuestionType"> </el-table-column>
        <el-table-column prop="question" label="题干" width="280"> </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="180">
          <template v-slot="{ row }">
            {{ row.addDate | parseTime }}
          </template>
        </el-table-column>
        <el-table-column :formatter="formatDifficulty" prop="difficulty" label="难度" width="80"> </el-table-column>
        <el-table-column prop="creator" label="录入人" width="120"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="{row}">
            <el-button @click="previewQuestion(row.id)" type="text" size="small">预览</el-button>
            <el-button type="text" size="small" @click="questionCheckFn(row.id)">审核</el-button>
            <el-button type="text" size="small" @click="$router.push('new')">修改</el-button>
            <el-button type="text" size="small"  @click="upJia(row)" v-html=" row.publishState ? '下架' : '上架' "></el-button>
            <el-button type="text" @click="open(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="end" align="middle">
        <el-pagination background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="page.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="page.total" >
        </el-pagination>
      </el-row>
      <!-- 审核 -->
      <el-dialog
      :visible.sync="showCheckDialog"
      title="题目审核"
      width="25%"
      >
  <el-radio v-model="radio" label="1">通过</el-radio>
  <el-radio v-model="radio" label="2">拒绝</el-radio>
  <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入审核意见"
  v-model="textarea"
  style="margin-top:20px ">
</el-input>
      </el-dialog>
    </el-card>
    <questions-preview
       :questionParticulars="questionParticulars"
       :show-dialog="showDialog"
       @update="showDialog = $event"
    ></questions-preview>
    <QuestionsCheck
    :show-check-dialog="showCheckDialog"
    :id="checkId"
    @update="showCheckDialog = false"
    ></QuestionsCheck>
  </div>
</template>

<script>
import { choice, remove, detail, choicePublish, choiceAdd } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects'
import { simple as lable } from '@/api/hmmm/tags.js'
import { provinceslist, cityslist } from '@/api/hmmm/citys'
import { questionType, difficulty } from '@/api/hmmm/constants'
import QuestionsPreview from '../components/questions-preview'
import QuestionsCheck from '../components/questions-check'
export default {
  components: {
    QuestionsPreview,
    QuestionsCheck
  },
  data () {
    return {
      showDialog: false, // 预览弹层的默认显示
      showCheckDialog: false,
      questionParticulars: {},
      checkId: null,
      upJiaOR: null,
      items: [], // 接收数组
      radio: '1', // 单选框
      textarea: '',
      page: {
        page: 1, // 当前页码
        pages: 1, // 页码大小
        pagesize: 10
      },
      counts: 0, // 总数
      loading: false, // 显示遮罩层
      options: [
        {
          value: '',
          label: ''
        }
      ],
      value: '',
      activeName: 'first',
      difficulty: [
        {
          value: 1,
          label: '简单'
        },
        {
          value: 2,
          label: '一般'
        },
        {
          value: 3,
          label: '困难'
        }
      ],
      questionType: [
        {
          value: 1,
          label: '单选'
        },
        {
          value: 2,
          label: '多选'
        },
        {
          value: 3,
          label: '简答'
        }
      ],
      direction: [
        {
          value: 1,
          label: 'o2o'
        },
        {
          value: 2,
          label: '外包服务'
        },
        {
          value: 3,
          label: '企业服务'
        },
        {
          value: 4,
          label: '互联网金融'
        },
        {
          value: 5,
          label: '企业咨询'
        },
        {
          value: 6,
          label: '互联网'
        },
        {
          value: 7,
          label: '电子商务'
        },
        {
          value: 8,
          label: '其他'
        }
      ],
      simples: [], // 学科
      lables: [], // 标签
      province: [], // 城市
      creators: [{ value: '超级管理员' }, { value: '录入管理员' }]
    }
  },
  created () {
    this.choice()
    this.simple()
    this.lable()
    this.provinces()
  },
  methods: {
    submitForm () {
      // console.log(this.page, 'a')
      this.choice()
    },
    async upJia (row) {
      if (row.publishState === 1) {
        this.upJiaOR = '下架'
      } else if (row.publishState === 0) {
        this.upJiaOR = '上架'
      } else {
        this.upJiaOR = '未知'
      }
      var data = {
        id: row.id,
        publishState: 1
      }
      if (row.publishState === 1) {
        data.publishState = 0
      } else if (row.publishState === 0) {
        data.publishState = 1
      }
      await this.$confirm('您确认' + this.upJiaOR + '这道题目吗?', '提示', {
        type: 'warning'
      })
      await choicePublish(data)
      await this.choice()
      this.$message.success('操作成功')
    },
    handleClick (tab, event) {
      switch (tab.label) {
        case '全部':
          this.page.chkState = undefined
          break
        case '待审核':
          this.page.chkState = 0
          break
        case '已通过':
          this.page.chkState = 1
          break
        case '已拒绝':
          this.page.chkState = 2
          break
      }
      this.choice()
      console.log(tab.label, event)
    },
    async choice () {
      this.loading = true
      const { data } = await choice(this.page)
      this.items = data.items
      this.counts = data.counts
      this.loading = false
    },
    async simple () {
      const { data } = await simple()
      this.simples = data
    },
    async lable () {
      const { data } = await lable()
      this.lables = data
    },
    async provinces () {
      this.province = await provinceslist()
    },
    provincesList (e) {
      this.provincesList = cityslist(e)
    },
    formatQuestionType (row, column, cellValue, index) {
      return questionType.map((item) => {
        if (item.value === cellValue) {
          return item.label
        }
      })
    },
    formatDifficulty (row, column, cellValue, index) {
      return difficulty.map((item) => {
        if (item.value === cellValue) {
          return item.label
        }
      })
    },
    async open (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        await this.choice() // 重新加载数据
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    questionCheckFn (id) {
      this.showCheckDialog = true
      this.checkId = id
    },
    async addChoicenessQuestion (id) {
      await this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        type: 'warning'
      })
      await choiceAdd({
        id,
        choiceState: 1
      })
      await this.choice()
      this.$message.success('题目加入精选成功')
    },
    async previewQuestion (id) {
      this.showDialog = true
      const { data } = await detail({ id })
      this.questionParticulars = data
      const dialogQuestionType = this.questionType.map(item => {
        if (item.value === this.questionParticulars.questionType) {
          return item.label
        }
      }).filter(item => item !== undefined)
      this.questionParticulars.questionType = dialogQuestionType[0]

      const dialogdifficulty = this.difficulty.map(item => {
        if (item.value === this.questionParticulars.difficulty) {
          return item.label
        }
      }).filter(item => item !== undefined)
      this.questionParticulars.difficulty = dialogdifficulty[0]
    }
  }
}
</script>

<style scoped >
.bg-purple-dark {
  background: rgb(244, 244, 245);
}
</style>
